package vikaa.ui;

import vikaa.wowo.R;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

public class WowoCreate2 extends Activity{
	private LinearLayout backBtn;
	private ImageView submitBtn;
	private EditText nameEdt;
	public static boolean isfinish = false;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.wowo_create2);
		initview();
		isfinish=false;
	}
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		if(isfinish){
			WowoCreate1.isfinish = true;
			finish();
		}
	}
	public void initview(){
		backBtn = (LinearLayout)findViewById(R.id.backBtn);
		backBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				finish();
			}
		});
		submitBtn = (ImageView)findViewById(R.id.submitBtn);
		submitBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if(nameEdt.getText().toString().length() >= 2){
					Intent i = new Intent(WowoCreate2.this,WowoCreate3.class);
					i.putExtra("name", nameEdt.getText().toString());
					i.putExtra("state", "0");
					startActivity(i);
				}
				else{
					Toast.makeText(getApplicationContext(), getString(R.string.name_rule), Toast.LENGTH_SHORT).show();
				}
			}
		});
		nameEdt = (EditText)findViewById(R.id.nameEdt);
		nameEdt.addTextChangedListener(mNameWatcher);
	}
	TextWatcher mNameWatcher = new TextWatcher() {
        private CharSequence temp;
        private int editStart ;
        private int editEnd ;
        public void beforeTextChanged(CharSequence s, int arg1, int arg2,
                int arg3) {
            temp = s;
        }
       
        public void onTextChanged(CharSequence s, int arg1, int arg2,
                int arg3) {
        }
       
        public void afterTextChanged(Editable s) {
            editStart = nameEdt.getSelectionStart();
            editEnd = nameEdt.getSelectionEnd();
            if (temp.length() > 6) {
                s.delete(editStart-1, editEnd);
                int tempSelection = editStart;
                nameEdt.setText(s);
                nameEdt.setSelection(tempSelection);
            }
            if(temp.length() == 6){
            	
            }
        }
    };
}
